package com.hyq.market.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.hyq.market.R;
import com.hyq.market.adapter.MyFragmentPagerAdapter;
import com.hyq.market.ui.home.gclass.ClassFt;
import com.hyq.market.ui.home.home.HomeFt;
import com.hyq.market.ui.home.mime.MineFt;
import com.hyq.market.ui.home.shopcar.ShopCarFt;
import com.hyq.market.ui.login.LoginActivity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HomeAc extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener {

    @BindView(R.id.frame_view)
    FrameLayout frameView;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.button_index)
    RadioButton buttonIndex;
    @BindView(R.id.button_class)
    RadioButton buttonClass;
    @BindView(R.id.button_car)
    RadioButton buttonCar;
    @BindView(R.id.button_mine)
    RadioButton buttonMine;
    RadioGroup radioGroup;
    private ArrayList<Fragment> fragmentList;
    private MyFragmentPagerAdapter mSectionsPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
        radioGroup= (RadioGroup) findViewById(R.id.parent_radiogroup);
        radioGroup.setOnCheckedChangeListener(this);
        viewPager.addOnPageChangeListener(this);
        initView();
    }

    private void initView() {

        initFragment();
        mSectionsPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList);
        viewPager.setOffscreenPageLimit(3);
        viewPager.setAdapter(mSectionsPagerAdapter);
    }


    void initFragment() {
        fragmentList = new ArrayList<>();
        fragmentList.add(new HomeFt());
        fragmentList.add(new ClassFt());
        fragmentList.add(new ShopCarFt());
        fragmentList.add(new MineFt());
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int checkedId) {
       switch (checkedId)
       {
           case R.id.button_index:
               viewPager.setCurrentItem(0);
               break;
           case R.id.button_class:
               viewPager.setCurrentItem(1);
               break;
           case R.id.button_car:
//               viewPager.setCurrentItem(2);
              //
                startActivity(new Intent(this, LoginActivity.class));
               break;
           case R.id.button_mine:
               viewPager.setCurrentItem(3);
             //  startActivity(new Intent(this, LoginActivity.class));
               break;
       }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position)
        {
            case 0:
                buttonIndex.setChecked(true);
                break;
            case 1:
                buttonClass.setChecked(true);
                break;
            case 2:
                buttonCar.setChecked(true);
                break;
            case 3:
                buttonMine.setChecked(true);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
